Skip to content

fix: honor Event(internal=True) when the event is triggered - #663

Closed
chiliec wants to merge 1 commit into
fgmacedo:developfrom
chiliec:fix/event-internal-at-trigger
Closed

chiliec wants to merge 1 commit into
fgmacedo:developfrom
chiliec:fix/event-internal-at-trigger

Conversation

@chiliec

@chiliec chiliec commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #655.

Event(a.to(b), internal=True) was preserved on the declaration but never reached the queue:

  • Event.__get__ built the BoundEvent without internal, so sm.go() always put on the external queue.
  • StateChart.send read delay from the declared event but not internal, so sm.send("go") ignored it too.

Fix: pass internal through in __get__, and in send fall back to the declared event's internal when the caller did not pass internal=True (same precedence as delay).

Per the issue notes: dropped the "internal still has no effect at trigger time" caveat from docs/releases/3.2.2.md, added a 3.2.2 release-notes entry, and listed the declarative form in the queue table in docs/processing_model.md.

Tests

TestExplicitEvent.test_declared_internal_puts_on_the_internal_queue (parametrized over sm.light() and sm.send("light")) records the internal flag passed to the engine's put. It fails on develop for both paths ([False]) and passes with this change.

  • uv run pytest: 2020 passed, 144 skipped, 44 xfailed
  • uv run ruff check / ruff format --check / mypy statemachine: clean

@sonarqubecloud

Copy link
Copy Markdown

@fgmacedo fgmacedo left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @chiliec, welcome! This is your first contribution here, thank you for taking the time to dig into #655, write it up so clearly and cover both trigger paths with a test that fails on develop.

Honoring the declaration surfaces two regressions for existing internal=True declarations: a declared delay stops being waited, and on the async engine the first trigger on a fresh machine is dropped. Details inline.

Comment thread statemachine/event.py
return self
return BoundEvent(id=self.id, name=self.name, delay=self.delay, _sm=instance)
return BoundEvent(
id=self.id, name=self.name, delay=self.delay, internal=self.internal, _sm=instance

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (blocking): Carrying internal through makes a declared delay silently inert: the internal queue never checks execution_time (only Phase 3 does), so Event(dark.to(lit), delay=50, internal=True) now fires at 0 ms, while on develop it waited 50 ms. That is exactly the declaration in test_accept_delay_and_internal and the example right above the new release note. Either hold internal triggers until their due time in both engines, or reject delay together with internal=True and say so in the note. raise_(..., delay=) has the same hole today, so a follow-up could cover it; #665 is rewriting the due-time checks, worth aligning with it.

delay = (
delay if delay else know_event and know_event.delay or 0
) # first the param, then the event, or 0
internal = internal or bool(know_event and know_event.internal)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (blocking): On the async engine the first await sm.go() on a fresh machine now drops the event: Phase 1 pops it from the internal queue while the configuration is still empty (the __initial__ external event has not run yet), so it matches nothing, go() returns None and the machine stays in a. On develop the same declaration worked because the event went external, behind __initial__. docs/async.md promises the first send() activates the initial state automatically. Activate the initial configuration before an internal put from outside the loop, or process __initial__ before draining the internal queue when the configuration is empty.

Comment thread docs/releases/3.2.2.md

`Event(a.to(b), internal=True)` kept `internal` on the declaration, but neither `sm.go()` nor
`sm.send("go")` carried it to the queue, so the event always went to the external queue. Both
paths now honor the declaration and put the event on the internal queue.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): Both engines collect the processing result only from external events, so sm.go() on a declared-internal event now returns None instead of the callback result (verified: on_go returning "go-result" gives None once internal=True is declared). A class-level flag changing the return contract of every caller is worth one sentence here.

Comment thread tests/test_events.py
assert BeaconsOfGondor().light.delay == 50

@pytest.mark.parametrize("trigger", ["method", "send"])
def test_declared_internal_puts_on_the_internal_queue(self, trigger):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): Run this through sm_runner so the async engine is covered too (CLAUDE.md, "Testing both sync and async engines"); the async path is where the declaration misbehaves (see the comment on send). A case that triggers before activate_initial_state() would have caught the dropped event.

Comment thread tests/test_events.py
@@ -71,6 +71,31 @@ class BeaconsOfGondor(StateChart):
assert BeaconsOfGondor.light.delay == 50
assert BeaconsOfGondor().light.delay == 50

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (non-blocking): The bound side of internal has no direct assertion.

Suggested change
assert BeaconsOfGondor().light.delay == 50
assert BeaconsOfGondor().light.delay == 50
assert BeaconsOfGondor().light.internal is True

@codecov

codecov Bot commented Oct 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (525bcdd) to head (e633508).

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #663   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           52        52           
  Lines         5582      5583    +1     
  Branches       883       883           
=========================================
+ Hits          5582      5583    +1     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fgmacedo

Copy link
Copy Markdown
Owner

Thanks again @chiliec. To get this into 3.2.2 without making you chase the engine issues from the review, I carried your commit, with your authorship, into #668 and built the rest on top of it: an internal event triggered from outside a callback now goes to the external queue (so it returns its result and works on a fresh async machine), delay together with internal is rejected with InvalidDefinition, and each external event now gets its own macrostep. Closing this one in favor of #668; your fix is credited in the 3.2.2 release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

internal declared on an Event has no effect at trigger time

2 participants